
'''
生成式
[i*2 for i in range(10)]
'''

'''
生成器(generator)，只有在调用时才会生成相应的数据
只记录当前位置
只有一个__next()__方法
(i*2 for i in range(10))
'''

def fib(max):
    n,a,b = 0,0,1
    while n < max:
        # print(b)
        yield b   #有yield，，就可以看成一个生成器，函数停在这一句
        a, b=b ,a+b  #表面意思a = b,b=a+b
        '''实际上
        t = (b,a+b) #t是一个tuple
        a = t[0]
        b = t[1]
        '''
        n = n+1
    return '----done----'#返回的错误信息
# fib(10)
#print(fib(10))  #<generator object fib at 0x000002618350F448>
f = fib(10)
g = fib(6)
while True:
    try:
        x = next(g)
        print('g:',x)
    except StopIteration as e:
        print('Generator return value:',e.value)
        break

'''print("-------")
print(f.__next__())
print('******')
print(f.__next__())
print('++++++++')
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
# print(f.__next__())    10个，，超出了
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
# print(f.__next__())
print('----start loop----')
for i in f:
    print(i)'''